package com.gper.edu.vip.builder.singleton;

/**
 * @Author: ellis.guan
 * @Description:
 * 1. 优点：写法优雅，利用java本身语法特点，性能高，避免了内存浪费
 * 2. 缺点：能够被反射破坏
 * @Date: 2020/2/26 21:34
 * @Version: 1.0
 */
public class LazyStaticInnerClassSingleton {

    private LazyStaticInnerClassSingleton(){
        //防止反射破坏，代码就得不优雅了
        if (LazyHolder.singleton != null){
            throw new RuntimeException("不允许非法访问!");
        }
    }

    public static LazyStaticInnerClassSingleton getInstance(){
        return LazyHolder.singleton;
    }

    /**
     * 内部类在类加载的时候不会分配空间，在使用时才会分配空间
     */
    private static class LazyHolder{
        private static final LazyStaticInnerClassSingleton singleton = new LazyStaticInnerClassSingleton();
    }
}
